using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Providers;
using BlogEngine.Core;

public partial class gallery_editphoto : System.Web.UI.Page
{
	protected Guid guid;

	protected void Page_Load(object sender, EventArgs e)
	{
		guid = new Guid(Request.QueryString["PhotoId"]);

		if (!Page.IsPostBack && !Page.IsCallback)
			BindPage();

		btnAlbum.Click += new EventHandler(btnPhoto_Click);

		valExist.ServerValidate += new ServerValidateEventHandler(valExist_ServerValidate);
	}

	#region Event Handlers

	private void valExist_ServerValidate(object source, ServerValidateEventArgs args)
	{
		args.IsValid = true;

		foreach (Photo photo in Photo.Items)
			if (photo.Title.Equals(txtTitle.Text.Trim(), StringComparison.OrdinalIgnoreCase))
				args.IsValid = false;
	}

	/// <summary>
	/// Saves the album
	/// </summary>
	private void btnPhoto_Click(object sender, EventArgs e)
	{
		if (Page.IsValid)
			if (txtTitle.Text.Trim().Length != 0)
			{
				Photo photo = Photo.GetById(guid);
				photo.Title = txtTitle.Text.Trim();
				photo.Description = txtDescription.Text.Trim();
				photo.IsPublished = cbIsPublished.Checked;
				photo.Slug = txtSlug.Text.Trim();
				photo.TagString = txtTags.Text;
				photo.Save();
			}
	}

	#endregion

	#region Data Binding

	private void BindPage()
	{
		Photo photo = Photo.GetById(guid);		
		txtTitle.Text = photo.Title;
		txtDescription.Text = photo.Description;
		txtSlug.Text = photo.Slug;
		cbIsPublished.Checked = photo.IsPublished;
		txtTags.Text = photo.TagString;
	}

	#endregion


}
